#include <iostream>                                     // Include header files
#include <cmath>    
#include <iomanip>

using namespace std;                                    

void calcInterest()                                     // Define calcInterest()
{
	double A = 0;                                       // Declare/ Initialize variables for calcIntertest();
	double n = 0;
	double i = 0;
	double P = 0;
	double X = 0;
	double Y = 0;
	
	cout << "What is the loan principal or the remaining balance owed (ex. 1000.00): ";		
	cin >> P;											// User Input - Loan Ammount / Principal

	cout << "What is the monthly interest rate on the loan (ex. .01 for 1%)? ";
	cin >> i;											// User Input - Monthly Interest Rate

	cout << "How many monthly payments remain to pay off the debt (ex. 12?: ";
	cin >> n;											// User Input - Loan Period
	cout << endl;

	A = P * (i + (i / (pow((1 + i), n) - 1)));			// formula to find A
	
	X = P / n;                                          // formulas for X and Y variables
	Y = (A - X) * n;
	cout << "If you pay off your loan over " << n << " months, you will pay $ " << Y;   // Output of Y
    cout << "more than if you payed off the debt today" << endl;
}
void calcYears()
{
	double A = 0;
	double i = 0;
	double P = 0;
	double n = 0;

	cout << "What is the loan principal or the remaining balance owed (ex. 1000.00)? ";		
	cin >> P;											// User Input - Loan Ammount / Principal

	cout << "What is the monthly interest rate on the loan (ex. .01 for 1%)? ";
	cin >> i;											// User Input - Monthly Interest Rate

	cout << "How much can you afford to pay each month? ";
	cin >> A;											// User Input - Monthly Payment
	cout << endl;

	n = ((1 + i) * P) / A;                              // Formula for n

	cout << "The amount of time it will take to pay off your loan, paying" << endl;                 // Output for n
	cout << "$" << A << "every month = " << n << "months, or" << (n / 12) << " years." << endl;
}                                                       // End calcYears()

void calcPrincipal()                                    // Define calcPrincipal()
{   
	double A = 0;                                       // Initialize variables for calcPrinicipal()
	double n = 0;
	double i = 0;
	double P = 0;

	cout << "What is your current monthly payment (ex. 100.00)? ";		
	cin >> A;											// User Input - Monthly Payment Amount

	cout << "What is the monthly interest rate on the loan (ex. .01 for 1%)? ";
	cin >> i;											// User Input - Monthly Interest Rate

	cout << "What is the loan period, in months (ex. 12): ";
	cin >> n;											// User Input - Loan Period
	cout << endl;

	P = A / ((i + (i / (pow((1 + i), n) - 1))));
	cout << "The Principal, or the current remaining balance is = " << P << endl;
}                                                       // End CalcPrincipal()

void calcPayment()		                                // Define calcPayment function/parameters
{														// Initialize Variables i, n, P, A 	
	double i = 0;
	double n = 0;
	double P = 0;
	double A = 0;

	cout << "What is the principal, or remaining balance on the loan (ex. 1000.00)? ";		
	cin >> P;											// User Input - Principal

	cout << "What is the monthly interest rate on the loan (ex. .01 for 1%)? ";
	cin >> i;											// User Input - Monthly Interest Rate

	cout << "What is the loan period, in months (ex. 12): ";
	cin >> n;											// User Input - Loan Period
	cout << endl;

	A = P * (i + (i / (pow((1 + i), n) - 1)));			// formula for A	
	
    if(A < 100)                                         // Split if loops for correct setprecision (hopefully)
    {
	    cout << "The required Monthly Payment: $" << setprecision(4) << A << endl;		// Program displays "A", final result.
	    cout << endl;
    }
    else if(A >= 100)
    {
        cout << "The required Monthly Payment: $" << setprecision(5) << A << endl;		// Program displays "A", final result.
	    cout << endl;
    }

}                                                       // End calcPayment()

void StudentLoanCalculator()                                                                     // Start int StudentLoanCalculator()
{	
	cout << "Welcome to The Student Loan Debt Calculator" << endl;								// Introduction
	cout << "Created by Philip Gladek" << endl << endl;									
	
	int choice = 1;                                                                             // Declare/ Initialize choice
	while(true)	                                                                                // Start While loop for calc menu                                                            								
	{
	cout << "Please select from the following options: " << endl << endl;                       // Output for calculator menu options
	cout << "1. What will my monthly payment be, to pay off my student loan?" << endl;
	cout << "2. How much do I still owe?" << endl;
	cout << "3. How many years will it take to pay off my student loan?" << endl;
	cout << "4. How much more will I pay in interest, making monthly payments, " << endl;
    cout << "vs. paying off the entire balance now?" << endl;
	cout << "0. Exit the Student Loan Debt Calculator." << endl;
	cin >> choice;                                                                              // User input for choice

	if(choice == 1)                                                                             // If loops for user choice
	{
		calcPayment();	                                                                        // call CalcPayment()						
	}
	if(choice == 2)
	{
		calcPrincipal();                                                                        // Call calcPrincipal()
	}
	if(choice == 3)
	{
		calcYears();                                                                            // Call calcYears()
	}
	if(choice == 4)
	{
		calcInterest();                                                                         // Call calcInterest()
	}
	if(choice == 0)
	{
		cout << "Thank you for using the Student Loan Debt Calculator. Good-Bye." << endl;
		break;                                                                                  // exit option
	}
	}
	system("PAUSE");                                                                            // system pause
}                                                                                               // End StudentLoanCalculator() / Main() Function